#include "libloader.h"
#include <QMetaMethod>
#include <iostream>
#include <stdio.h>
#include <qwidget.h>
#include "messanger.h"
#include <QApplication>

int main(int argc, char** argv) {

    QApplication a(argc, argv);

    LibLoader* loader = LibLoader::getInstance();

    loader->loadLib("example");

    Messager *m = new Messager();
    QObject::connect(loader, SIGNAL(warning(QString)), m, SLOT(showWarning(QString)));

    const MetaClass* clas = loader->getClass("Calc", "example");
    if (clas) {
        qDebug() << "metaClass found \n";

        QObject *o = clas->instantiate();

        if (o) {
            qDebug() << "instance is created \n";
        } else {
            qDebug() << "instance NOT created \n";
        }

        const QMetaObject* metaObject = o->metaObject();

        metaObject->connect(o, metaObject->indexOfSignal("message(int)"), m, m->metaObject()->indexOfSlot("showMessageI(int)"));
        for (int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); i++)
        {
            QMetaProperty m = metaObject->property(i);
            qDebug() << m.typeName() << " " << m.name();
        }

        for(int i = metaObject->methodOffset(); i < metaObject->methodCount(); ++i)
        {
            QMetaMethod m = metaObject->method(i);
            qDebug() << m.signature();
        }

        int v;
        metaObject->property(metaObject->indexOfProperty("operA")).write(o, 10);
        metaObject->property(metaObject->indexOfProperty("operB")).write(o, 20);
        metaObject->invokeMethod(o, "addFunc", Qt::DirectConnection,
                                 Q_RETURN_ARG(int, v),
                                 Q_ARG(int, 42));
        qDebug() << "returned " << v;

    } else {
        qDebug() << "metaClass NOT found";
    }

    const MetaClass* clas2 = loader->getClass("Calc", "tlib");
    if (clas2) {
        qDebug() << "metaClass found \n";

        QObject *o = clas2->instantiate();

        if (o) {
            qDebug() << "instance is created \n";
        } else {
            qDebug() << "instance NOT created \n";
        }

        const QMetaObject* metaObject = o->metaObject();

        for(int i = metaObject->methodOffset(); i < metaObject->methodCount(); ++i)
        {
            QMetaMethod m = metaObject->method(i);
            qDebug() << m.signature();
        }

    } else {
        qDebug() << "metaClass NOT found";
    }

    const MetaClass* clas3 = loader->getClass("Calc");
    if (clas3) {
        qDebug() << "metaClass found \n";

        QObject *o = clas3->instantiate();

        if (o) {
            qDebug() << "instance is created \n";
        } else {
            qDebug() << "instance NOT created \n";
        }

        const QMetaObject* metaObject = o->metaObject();

        for(int i = metaObject->methodOffset(); i < metaObject->methodCount(); ++i)
        {
            QMetaMethod m = metaObject->method(i);
            qDebug() << m.signature();
        }

    } else {
        qDebug() << "metaClass NOT found";
    }

    getchar();

    return a.exec();
}
